<?php
/**
 * Created by PhpStorm.
 * User: xi
 * Date: 2020-05-02
 * Time: 16:54
 */

namespace app\api\service;


use app\admin\model\Air;
use app\admin\model\Roomclean;
use app\admin\model\Shop;
use app\admin\model\Shopdevice;
use app\admin\model\Shopks;
use app\admin\model\Shoporderprofit;
use app\admin\model\Shoproombook;
use app\admin\model\Shopstaff;
use app\admin\model\Shoptable;
use app\admin\model\Userorderprofit;
use app\admin\model\Usertuan;
use app\admin\model\Userwithdraw;
use fast\Http;

class KdService
{

    public function queryKd($param)
    {

        $host = "https://wuliu.market.alicloudapi.com";//api访问链接
        $path = "/kdi";//API访问后缀
        $method = "GET";
        $appcode = "f7b9abf08c4d43e5aa9053eb1073fcb4";//开通服务后 买家中心-查看AppCode
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $appcode);
        $querys = "no=" . $param['kd_num'];
        $bodys = "";
        $url = $host . $path . "?" . $querys;

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, true);
        if (1 == strpos("$" . $host, "https://")) {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        $out_put = curl_exec($curl);

        $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);

        list($header, $body) = explode("\r\n\r\n", $out_put, 2);
        $ret = array();
        if ($httpCode == 200) {

            $ret['code'] = 1;
            $ret['data'] = $body;
            $ret['msg'] = "";
            return $ret;

        } else {
            if ($httpCode == 400 && strpos($header, "Invalid Param Location") !== false) {
                $ret['code'] = 0;
                $ret['data'] = "";
                $ret['msg'] = "参数错误";
            } elseif ($httpCode == 400 && strpos($header, "Invalid AppCode") !== false) {
                $ret['code'] = 0;
                $ret['data'] = "";
                $ret['msg'] = "AppCode错误";
            } elseif ($httpCode == 400 && strpos($header, "Invalid Url") !== false) {
                $ret['code'] = 0;
                $ret['data'] = "";
                $ret['msg'] = "请求的 Method、Path 或者环境错误";
            } elseif ($httpCode == 403 && strpos($header, "Unauthorized") !== false) {
                $ret['code'] = 0;
                $ret['data'] = "";
                $ret['msg'] = "服务未被授权（或URL和Path不正确）";
            } elseif ($httpCode == 403 && strpos($header, "Quota Exhausted") !== false) {
                $ret['code'] = 0;
                $ret['data'] = "";
                $ret['msg'] = "套餐包次数用完";
            } elseif ($httpCode == 403 && strpos($header, "Api Market Subscription quota exhausted") !== false) {
                $ret['code'] = 0;
                $ret['data'] = "";
                $ret['msg'] = "套餐包次数用完，请续购套餐";
            } elseif ($httpCode == 500) {
                $ret['code'] = 0;
                $ret['data'] = "";
                $ret['msg'] = "API网关错误";
            } elseif ($httpCode == 0) {
                $ret['code'] = 0;
                $ret['data'] = "";
                $ret['msg'] = "URL错误";
            } else {
                $ret['code'] = 0;
                $ret['data'] = "";
                $ret['msg'] = "未知错误";
            }

            return $ret;
        }
    }


}